package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hddz.acquisition.R;

public class MessageAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<String> mListItem;
	private Context mContext;
	private ListView listView;

	public MessageAdapter(Context context, ListView listView) {
		super();
		this.mInflater = LayoutInflater.from(context);
		this.mListItem = new ArrayList<String>();
		this.listView = listView;
		this.mContext = context;
	}

	public void appandItem(String msg) {
		mListItem.add(0, msg);
		listView.setSelection(0);
		listView.smoothScrollToPosition(0);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListItem.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mListItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.view_stock_msg_list_item, null);
			holder = new Holder();
			holder.mIndex = (TextView) convertView.findViewById(R.id.txt_index);
			holder.mMsg = (TextView) convertView.findViewById(R.id.txt_msg);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.mIndex.setText((position + 1) + "");
		holder.mMsg.setText(mListItem.get(position));

		return convertView;
	}

	class Holder {
		private TextView mIndex, mMsg;
	}
}
